package com.analytical.www.component

import android.content.Context
import android.media.SoundPool
import android.os.Build
import androidx.annotation.RequiresApi
import com.analytical.www.R

/**
 * @author Secret
 * @since 2023/5/22
 */
object SoundPoolManager {

    private lateinit var soundPool: SoundPool
    private val soundMap = HashMap<Int, Int>()


    fun init(context: Context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            soundPool = SoundPool.Builder().setMaxStreams(10).build()
            soundMap[R.raw.tap_button] = soundPool.load(context, R.raw.tap_button, 1)
        }
    }

    fun showSound(rawId: Int) {
        val soundId = soundMap[rawId]
        soundId?.let {
            soundPool.play(it, 1f, 1f, 1, 0, 1f)
        }
    }

}